<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active{
      color: rgb(231, 86, 29);
    }
    .news-page .router-link-exact-active{
      color: rgb(52, 201, 161);
    }
  </style>
</head>
<body>
  <div id="app">
    <router-link to="/home">首页</router-link>
    <router-link to="/news">新闻页</router-link>
    <router-link to="/about" replace>关于我们页</router-link>
    <hr>
    <!-- 视图上定义 router-view 作为路由组件的出口 -->
    <router-view></router-view>
  </div>
  <script src="./vue.global.js"></script>
  <script src="./vue-router.global.js"></script>
  <script>
    const { createApp } = Vue;
    const app = createApp();
    // 定义好路由组件的 config
    const HomePage = {
      template: `
        <div>
          <h1>这是首页</h1>  
        </div>
      `
    }
    const NativeNews =  {
      template: `
        <div>
          <h2>国内新闻</h2>  
        </div>
      `
    }
    const AbroadNews =  {
      template: `
        <div>
          <h2>海外新闻</h2>  
        </div>
      `
    }
    const NewsPage = {
      template: `
        <div class="news-page">
          <h1>这是新闻页</h1>  
          <router-link to="/news/native">国内新闻</router-link>
          <router-link to="/news/abroad">国外新闻</router-link>
          <hr/>
          <router-view></router-view>
        </div>
      `,
      mounted () {
        console.log(this.$route);
      }
    }
    const AboutPage = {
      template: `
        <div>
          <h1>这是关于我们页</h1>  
        </div>
      `
    }
    const NotFound = {
      template: `
        <div>
          <h1>阿欧，404啦</h1>  
        </div>
      `
    }
    // 创建路由实例
    const routes = [
      // 单个路由规则 至少有两个属性  path 路径 component 当前路由对应路由组件
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        component: HomePage
      },
      {
        path: '/news',
        component: NewsPage,
        // 在父级路由规则定义 children属性 定义子路由规则
        children: [
          {
            path: '/news',
            redirect: '/news/native'
          },
          {
            path: '/news/native', // 最好携带 一级路由path作为前缀 语义
            component: NativeNews
          },
          {
            path: '/news/abroad', 
            component: AbroadNews
          }
        ]
      },
      {
        path: '/about',
        component: AboutPage
      },
      {
        path: '/:pathMatch(.*)*',
        component: NotFound
      }
    ]
    const router = VueRouter.createRouter({
      // 路由模式，这里使用的是hash 模式
      history: VueRouter.createWebHashHistory(),
      // 路由规则
      routes
    })
    // 使用路由插件
    app.use(router);
    app.mount('#app')
   
  </script>
</body>
</html>